package cn.nubia.admin.application.service;

import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.application.bean.TopicSoftMap;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2015/7/10.
 */
@Service
public class TopicSoftMapService extends BaseServiceImpl<TopicSoftMap> {

    @Autowired
    SystemLogService systemLogService;

    public boolean saveSoftMap(TopicSoftMap softMap) {
        Integer currTime = DateUtil.getCurrentTime();
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        softMap.setLastModifyTime(currTime);
        TopicSoftMap topicSoftMap = getSoftMapBySoftId(softMap.getSoftId());
        if(topicSoftMap==null){
            //新增
            softMap.setCreateTime(currTime);
            softMap.setCreateAdminId(adminId);
            softMap.setCreateAdminName(adminName);
            softMap.setIsDelete(0);
            insert(softMap);
            systemLogService.log(SystemLog.ModuleTypeEnum.TOPICMAPLIST, SystemLog.OperateTypeEnum.ADD, softMap.getId());

        }else{
            softMap.setId(topicSoftMap.getId());
            softMap.setCreateAdminName(topicSoftMap.getCreateAdminName());
            softMap.setCreateTime(topicSoftMap.getCreateTime());
            softMap.setCreateAdminId(topicSoftMap.getCreateAdminId());
            softMap.setModifyAdminName(adminName);
            softMap.setModifyAdminId(adminId);
            update(softMap);

            systemLogService.log(SystemLog.ModuleTypeEnum.TOPICMAPLIST, SystemLog.OperateTypeEnum.EDIT, softMap.getId());

        }
        return true;
    }

    public TopicSoftMap getSoftMapBySoftId(Integer softId){
        Query<TopicSoftMap> topicSoftMapQuery = Query.build(TopicSoftMap.class);
        topicSoftMapQuery.addEq(TopicSoftMap.COLUMN_SOFTID, softId);
        topicSoftMapQuery.addNotEq(TopicSoftMap.COLUMN_ISDELETE,TopicSoftMap.DELETE);
        return get(topicSoftMapQuery);
    }

    public List<TopicSoftMap> listTopicSoftMaps(Integer topicId) {
        String sql = "SELECT a.*,b.soft_name as soft_name FROM nb_topic_soft_map a ,nb_soft b WHERE a.soft_id = b.soft_id " +
                " AND a.topic_id=" +topicId +
                " AND a.is_delete!=1" +
                " AND a.last_modify_time != 0" +
                " ORDER BY a.last_modify_time desc";
        List<TopicSoftMap> topicSoftMaps = findBySql(sql, TopicSoftMap.class);
        return topicSoftMaps;
    }

    public boolean deleteSoftMap(Integer id) {
        TopicSoftMap domain = get(id);
        domain.setIsDelete(TopicSoftMap.DELETE);
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        Integer curr = DateUtil.getCurrentTime();
        domain.setModifyAdminName(adminName);
        domain.setModifyAdminId(adminId);
        domain.setLastModifyTime(curr);
        update(domain);
        systemLogService.log(SystemLog.ModuleTypeEnum.TOPICMAPLIST, SystemLog.OperateTypeEnum.DEL, id);

        return true;
    }
}
